package com.jshan.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshan.base.entity.SysRole;
import com.jshan.service.SysRoleService;
import com.jshan.base.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jshan.common.annotation.OperationLog;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import java.util.List;

@Tag(name = "角色管理接口")
@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    // @OperationLog("查询角色列表")
    // @GetMapping("/list")
    // public Result<IPage<SysRole>> list(
    // @RequestParam(defaultValue = "1") Integer pageNum,
    // @RequestParam(defaultValue = "10") Integer pageSize,
    // @RequestParam(required = false) String roleName) {
    // return Result.ok(roleService.getRolePage(new Page<>(pageNum, pageSize),
    // roleName));
    // }

    @Operation(summary = "角色分页列表")
    @GetMapping("/page")
    @SaCheckPermission("system:role:list")
    public Result<IPage<SysRole>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String roleName) {
        return Result.ok(roleService.getRolePage(new Page<>(pageNum, pageSize), roleName));
    }

    @GetMapping("/{id}")
    public Result<SysRole> getById(@PathVariable Long id) {
        return Result.ok(roleService.getById(id));
    }

    @OperationLog("新增角色")
    @PostMapping
    @SaCheckPermission("system:role:add")
    public Result<Void> add(@RequestBody SysRole role) {
        roleService.save(role);
        return Result.ok();
    }

    @OperationLog("修改角色")
    @PutMapping
    @SaCheckPermission("system:role:edit")
    public Result<Void> update(@RequestBody SysRole role) {
        roleService.updateById(role);
        return Result.ok();
    }

    @OperationLog("删除角色")
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:role:delete")
    public Result<Void> delete(@PathVariable Long id) {
        roleService.removeById(id);
        return Result.ok();
    }

    @OperationLog("获取角色菜单ID")
    @GetMapping("/{roleId}/menuIds")
    @SaCheckPermission("system:role:auth")
    public Result<List<Long>> getRoleMenuIds(@PathVariable Long roleId) {
        List<Long> menuIds = roleService.getRoleMenuIds(roleId);
        return Result.ok(menuIds);
    }

    @OperationLog("更新角色菜单")
    @PutMapping("/{roleId}/menus")
    @SaCheckPermission("system:role:auth")
    public Result<Void> updateRoleMenus(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        roleService.updateRoleMenus(roleId, menuIds);
        return Result.ok();
    }
}
